package com.anyun.testdemo;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.anyun.thirdparty.IThirdPartyCallback;
import com.anyun.thirdparty.IThirdPartyCase;

import java.lang.reflect.Method;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private final static String TAG = MainActivity.class.getCanonicalName();
    private static final String RTMP_APP = "com.anyun.thirdparty";
    IThirdPartyCase callThirdParty;
    private Intent intent;

    private IBinder.DeathRecipient mDeathRecipient = new IBinder.DeathRecipient() {
        @Override
        public void binderDied() {
            Log.i(TAG, "binderDied: ");
            if (callThirdParty == null){
                Log.i(TAG, "binderDied: rmeote==null");
                return;
            }
            callThirdParty.asBinder().unlinkToDeath(this,0);
            callThirdParty = null;
            bindService(intent,serviceConnection,BIND_AUTO_CREATE);
        }
    };

    ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.i(TAG, "onServiceConnected: ");
            callThirdParty = IThirdPartyCase.Stub.asInterface(service);
            try {
                service.linkToDeath(mDeathRecipient,0);
                callThirdParty.registeCallback(iThirdPartyCallback);

            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i(TAG, "onServiceDisconnected: ");


        }
    };

    IThirdPartyCallback iThirdPartyCallback = new IThirdPartyCallback.Stub() {
        @Override
        public void onResult(String res) throws RemoteException {
            Log.i(TAG, "onResult: "+res);
        }


    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         intent = new Intent();
        intent.setComponent(new ComponentName(RTMP_APP,"com.anyun.thirdparty.FloatingService"));


        findViewById(R.id.btn_start).setOnClickListener(this);
        findViewById(R.id.btn_stop).setOnClickListener(this);
        findViewById(R.id.btn_startr).setOnClickListener(this);
        findViewById(R.id.btn_stopr).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_start:
                try {
                    if (callThirdParty!=null){

                    callThirdParty.start(1,"");
                    }
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btn_stop:
                try{
                    callThirdParty.stop(1);
                }catch (RemoteException e){

                }
                break;
            case R.id.btn_startr:
                bindService(intent,serviceConnection,BIND_AUTO_CREATE);
                break;
            case R.id.btn_stopr:
                if (callThirdParty != null && callThirdParty.asBinder().isBinderAlive()){
                    try {
                        callThirdParty.asBinder().unlinkToDeath(mDeathRecipient,0);
                        callThirdParty.unregisteCallback(iThirdPartyCallback);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
                unbindService(serviceConnection);
//                KillRTMPProcess();
                break;
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
//        unbindService(serviceConnection);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy: ");
        if (callThirdParty != null && callThirdParty.asBinder().isBinderAlive()){
            try {
                callThirdParty.asBinder().unlinkToDeath(mDeathRecipient,0);
                callThirdParty.unregisteCallback(iThirdPartyCallback);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
        unbindService(serviceConnection);
        KillRTMPProcess();

    }
    private void KillRTMPProcess() {
        ActivityManager manager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
        try {
            Method method = Class.forName("android.app.ActivityManager").getMethod("forceStopPackage", String.class);
            method.invoke(manager, RTMP_APP);

            iThirdPartyCallback = null;
            callThirdParty = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
